home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue29 / construc / DRBOBCGI.PAS next >
Encoding:
Pascal/Delphi Source File  |  1997-11-27  |  2.0 KB  |  95 lines

  1. unit DrBobCGI;
  2. {$I-}
  3. interface
  4. var
  5.   ContentLength: Integer = 0;
  6.  
  7.   function Value(const Field: ShortString): ShortString;
  8.   { use this function to get the CGI inputquery values }
  9.  
  10. implementation
  11. uses
  12.   SysUtils, Windows;
  13.  
  14. var
  15.   Data: String = '';
  16.  
  17.   function Value(const Field: ShortString): ShortString;
  18.   var
  19.     i: Integer;
  20.   begin
  21.     Result := '';
  22.     i := Pos(Field+'=',Data);
  23.     if i > 0 then
  24.     begin
  25.       Inc(i,Length(Field)+1);
  26.       while Data[i] <> '&' do
  27.       begin
  28.         Result := Result + Data[i];
  29.         Inc(i)
  30.       end
  31.     end
  32.   end {Value};
  33.  
  34. var
  35.   P: PChar;
  36.   i: Integer;
  37.   Str: ShortString;
  38. type
  39.   TRequestMethod = (Unknown,Get,Post);
  40. var
  41.   RequestMethod: TRequestMethod = Unknown;
  42.  
  43.  
  44. initialization
  45.   P := GetEnvironmentStrings;
  46.   while P^ <> #0 do
  47.   begin
  48.     Str := StrPas(P);
  49.     if Pos('REQUEST_METHOD=',Str) > 0 then
  50.     begin
  51.       Delete(Str,1,Pos('=',Str));
  52.       if Str = 'POST' then RequestMethod := Post
  53.       else
  54.         if Str = 'GET' then RequestMethod := Get
  55.     end;
  56.     if Pos('CONTENT_LENGTH=',Str) = 1 then
  57.     begin
  58.       Delete(Str,1,Pos('=',Str));
  59.       ContentLength := StrToInt(Str)
  60.     end;
  61.     if Pos('QUERY_STRING=',Str) > 0 then
  62.     begin
  63.       Delete(Str,1,Pos('=',Str));
  64.       SetLength(Data,Length(Str)+1);
  65.       Data := Str
  66.     end;
  67.     Inc(P, StrLen(P)+1)
  68.   end;
  69.   if RequestMethod = Post then
  70.   begin
  71.     SetLength(Data,ContentLength+1);
  72.     for i:=1 to ContentLength do read(Data[i]);
  73.     Data[ContentLength+1] := '&';
  74.   { if IOResult <> 0 then { skip }
  75.   end;
  76.   i := 0;
  77.   while i < Length(Data) do
  78.   begin
  79.     Inc(i);
  80.     if Data[i] = '+' then Data[i] := ' ';
  81.     if (Data[i] = '%') then { special code }
  82.     begin
  83.       Str := '$00';
  84.       Str[2] := Data[i+1];
  85.       Str[3] := Data[i+2];
  86.       Delete(Data,i+1,2);
  87.       Data[i] := Chr(StrToInt(Str))
  88.     end
  89.   end;
  90.   if i > 0 then Data[i+1] := '&'
  91.            else Data := '&'
  92. finalization
  93.   Data := ''
  94. end.
  95.